package algorithm.poj.p1000;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLDecoder;
import java.util.StringTokenizer;

/**
 * 分析：
 * 经典算法
 * 参考：http://wenku.baidu.com/view/d2694ffe04a1b0717fd5dd80.html
 * 实现：
 * 经验：
 * 教训：
 * 
 * 
 * @author wong.tong@gmail.com
 *
 */
public class P1833 {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {

		InputStream input = null;
		if (false) {
			input = System.in;
		} else {
			URL url = P1833.class.getResource("P1833.txt");
			File file = new File(URLDecoder.decode(url.getPath(), "UTF-8"));
			input = new FileInputStream(file);
		}
		
		BufferedReader stdin = new BufferedReader(new InputStreamReader(input));

		StringBuffer sb = new StringBuffer("");
		StringTokenizer st = null;
		String line = stdin.readLine();
		int m = Integer.valueOf(line.trim());
		for (int i = 0; i < m; i ++) {
			st = new StringTokenizer(stdin.readLine());
			int n = Integer.valueOf(st.nextToken());
			int k = Integer.valueOf(st.nextToken());
			st = new StringTokenizer(stdin.readLine());
			int[] ds = new int[n];
			for (int j = 0; j < n; j ++) {
				ds[j] = Integer.valueOf(st.nextToken());
			}
			int t = 0;
			while (t < k) {
				ds = next_permutation(ds);
				t ++;
			}
			for (int j = 0; j < ds.length; j ++) {
				if (j == 0) {
					sb.append(ds[j]);
				} else {
					sb.append(" ").append(ds[j]);
				}
			}
			sb.append("\n");
		}
		System.out.println(sb);
	}

	private static int[] next_permutation(int[] ds) {
		
		int j = ds.length-2;
		for (; j > -1; j --) {
			if (ds[j+1] > ds[j]) break;
		}
		if (j == -1) {
			for (int i = 0; i < (ds.length+1)/2; i ++) {
				int s = ds[ds.length-1-i];
				ds[ds.length-1-i] = ds[i];
				ds[i] = s;
			}
		} else {
			int k = ds.length-1;
			for (; k > -1; k --) {
				if (ds[k] > ds[j]) break;
			}
			int t = ds[k];
			ds[k] = ds[j];
			ds[j] = t;
			for (int i = j+1; i < (ds.length+j)/2+1; i ++) {
				int s = ds[ds.length+j-i];
				ds[ds.length+j-i] = ds[i];
				ds[i] = s;
			}
		}
		return ds;
	}
}